package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Comment;
import com.itheima.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author xuchangshuo
 * @date 2020/8/4 20:28
 */

@RestController
@CrossOrigin
@RequestMapping("/web/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    //添加评论
    @RequestMapping("/add")
    public Result add(@RequestBody Map<String, Object> map) {
        commentService.add(map);
        return new Result("success", "添加成功");
    }


    //关闭评论邮件通知
    @RequestMapping("/closeEmailNotification/{userUid}")
    public Result closeEmailNotification(@PathVariable("userUid") String userUid) {
        commentService.closeEmailNotification(userUid);
        return new Result("success", "关闭成功");
    }

    //删除评论
    @RequestMapping("/delete")
    public Result delete(@RequestBody Map<String, Object> commentVO) {
        commentService.delete(commentVO);
        return new Result("success", "删除成功");
    }

    //获取评论列表
    @RequestMapping("/getList")
    public Result getList(@RequestBody Map<String, Object> commentVO) {
        PageResult<Comment> comments = commentService.findAll(commentVO);
//        PageResult<Comment> result = new PageResult<>();
//        result.setRecords((List<Comment>) comments);
//        result.setTotal(comments.getTotal());
//        result.setCurrent((long) comments.get());
//        result.setOptimizeCountSql(true);
//        result.setSearchCount(true);
        return new Result("success", comments);
    }

    //获取用户的评论列表和回复
    @RequestMapping("/getListByUser")
    public Result getListByUser(@RequestBody Map<String, Object> userVO) {
        Page<Comment> comments = commentService.getListByUser(userVO);
        PageResult<Comment> result = new PageResult<>();
        result.setRecords(comments.getResult());
        result.setTotal(comments.getTotal());
        result.setSize((long) comments.getPageSize());
        result.setCurrent((long) comments.getPageNum());
        result.setOptimizeCountSql(true);
        result.setSearchCount(true);
        return new Result("success", result);
    }

    //获取用户点赞信息
    @RequestMapping("/getPraiseListByUser")
    public Result getPraiseListByUser(long currentPage, long pageSize) {
        Page<Comment> praiseListByUser = commentService.getPraiseListByUser(currentPage, pageSize);
        PageResult<Comment> result = new PageResult<>();
        result.setRecords(praiseListByUser);
        result.setTotal(praiseListByUser.getTotal());
        result.setSize((long) praiseListByUser.getPageSize());
        result.setCurrent((long) praiseListByUser.getPageNum());
        result.setOptimizeCountSql(true);
        result.setSearchCount(true);
        return new Result("success", result);
    }

    //举报评论
    @RequestMapping("/report")
    public Result report(@RequestBody Map<String, Object> map) {
        commentService.report(map);

        return new Result("success", "举报成功");
    }
}
